package com.game.service.game.turntable.control;

import com.game.service.app.PostJson;
import com.game.service.app.Utils;
import com.game.service.app.turntable.PlayingInfo;
import com.game.service.app.turntable.PlayingModel;
import com.game.service.comm.AESUtils;
import com.game.service.comm.EncipheredData;
import com.game.service.comm.RSAUtils;
import com.game.service.db.UserScoreDaoImpl;
import com.game.service.game.turntable.GameResult;
import com.game.service.game.turntable.TurntableAddConfig;
import com.game.service.socket.SocketServer;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

import static com.game.service.define.RoomConfig.*;

public class PrintGameRunInfo {
    //TODO 查询系统数据库里面盈利情况,总盈利,总返奖率,总活动赠送，总兑换钻石,

    public void getGameRunInfo() {
        List<PostJson> allList = UserScoreDaoImpl.getInstance().getGameRunInfoList(GAME_ID);
        ALlGameRunInfo(allList);
        List<PostJson> todayList = UserScoreDaoImpl.getInstance().getGameRunInfoList(GAME_ID, Utils.getTodayStart(), Utils.getTodayEnd());
        ALlGameRunInfo(todayList);

    }

    private void ALlGameRunInfo(List<PostJson> list) {
        GameRunInfo runInfo = new GameRunInfo();

        int playCounts = 0;

        for (PostJson postJson : list) {
            ////解密数据
            String jsonString = "{}";
            try {
                long start = System.currentTimeMillis();
                String ciphertext = postJson.getPostJson();
                EncipheredData encipheredData = new Gson().fromJson(ciphertext, EncipheredData.class);
                String RSAKey = encipheredData.getCipherKey();
                String AESCipherContent = encipheredData.getCipherContent();
                String AESCipherKey = RSAUtils.decrypt(Base64.getDecoder().decode(RSAKey));
                SocketServer.getKernel().printTime("RSA解密" + AESCipherKey, start);
                jsonString = AESUtils.decrypt(AESCipherContent, AESCipherKey);
                SocketServer.getKernel().printTime("AES解密", start);
            } catch (Exception e) {

            }

//            PlayingModel playingModel = new Gson().fromJson(jsonString, PlayingModel.class);
//            PlayingInfo playingInfo = playingModel.getResult();
//
//            int index = playingInfo.getItem();
//            switch (index) {
//                case 0xFFFF:
//                    runInfo.setDailySendDiamondAdd(playingInfo.getPrize());//每日奖励统计
//                    break;
//                case 0xFFFE:
//                    runInfo.setLuckyStoreExDiamondAdd(playingInfo.getPrize());//兑换统计
//                    break;
//                case 0:
//                case 1:
//                case 2:
//                case 3:
//                case 4:
//                case 5:
//                case 6:
//                case 7:
//                case 8:
//                case 9:
//                case 10:
//                case 11:
//                case 12:
//                case 13:
//                case 14:
//                case 15:
//                    runInfo.setGamePrizeTotalAdd(playingInfo.getPrize());//游戏奖励统计
//                    runInfo.setGameBetTotalAdd(playingInfo.getFee());//游戏门票统计
//                    try {
//                        GameResult result = convert((LinkedTreeMap<String, Object>) playingInfo.getRemarks(), GameResult.class);
//                        runInfo.getBetCount()[result.betid] += 1;//统计押注信息
//                        runInfo.getPrizeItemCount()[result.prizeid] += 1;//出奖信息统计
//                        final int itemStatus = result.itemStatus;
//                        if (itemStatus != 0) {
//                            runInfo.getPrizeItemConfigCount()[result.prizeid] += 1;
//                        }
//
//                        switch (itemStatus) {
//                            case 1:
//                                runInfo.getConfigBenefitKill().setCountsAdd();
//                                runInfo.getConfigBenefitKill().setBenefitAdd(result.fee);//获利
//                                runInfo.getConfigBenefitKill().getBetCount()[result.betid] += 1;//统计抽杀
//                                break;
//                            case 2:
//                                runInfo.getConfigBenefitProtect().setCountsAdd();
//                                runInfo.getConfigBenefitProtect().setBenefitAdd(result.prize);//赠送
//                                runInfo.getConfigBenefitProtect().getPrizeItemCount()[result.prizeid] += 1;//出奖信息统计
//                                break;
//                            case 3:
//                                runInfo.getConfigBenefitSaving().setCountsAdd();
//                                runInfo.getConfigBenefitSaving().setBenefitAdd(result.prize);//赠送
//                                runInfo.getConfigBenefitSaving().getPrizeItemCount()[result.prizeid] += 1;//出奖信息统计
//                                break;
//
//                            default:
//                                break;
//                        }
//
//
//                        playCounts += 1;
//                    } catch (Exception e) {
//                        throw new RuntimeException(e);
//                    }
//
//                    break;
//                default:
//
//                    break;
//
//            }
        }
        runInfo.setPrizeCounts(list.size() - playCounts);
        runInfo.setPlayCounts(playCounts);
        runInfo.print();
    }

    public static <T> T convert(LinkedTreeMap<String, Object> map, Class<T> clazz) throws Exception {
        T object = clazz.getDeclaredConstructor().newInstance();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            if (map.containsKey(field.getName())) {
                Object value = map.get(field.getName());
                if (field.getType().equals(String.class)) {
                    field.set(object, value.toString());
                } else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
                    field.set(object, ((Number) value).intValue());
                }
                // Add more type conversions as necessary
            }
        }
        return object;
    }


}
